package com.lsa.letcode.the141.linkedListCycle;

import static org.junit.Assert.*;

import org.junit.Test;

import com.lsa.letcode.common.LetCodeTestBase;
import com.lsa.letcode.common.ListNode;

public class LinkedListCycleTest extends LetCodeTestBase {
	@Test
	public void test() throws Exception {
		Solution s = new Solution();
		assertTrue(s.hasCycle(encycle(asList(1))));
		assertTrue(s.hasCycle(encycle(asList(1,2))));
		assertTrue(s.hasCycle(encycle(asList(1,2,3))));
		assertTrue(s.hasCycle(encycle(asList(1,2,3,4))));
		assertTrue(s.hasCycle(encycle(asList(1,2,3,4,5))));
		assertFalse(s.hasCycle(asList(1)));
		assertFalse(s.hasCycle(asList(1,2)));
		assertFalse(s.hasCycle(asList(1,2,3)));
		assertFalse(s.hasCycle(asList(1,2,3,4)));
		assertFalse(s.hasCycle(asList(1,2,3,4,5)));
	} 
	
	private ListNode encycle(ListNode n) {
		ListNode first = n;
		ListNode prev = n;
		n = n.next;
		while(n != null){
			prev = n;
			n = n.next;
		}
		
		prev.next = first;
		return first;
	}
}
